import matplotlib.pyplot as plt
import plotly.graph_objects as go
import pandas as pd
import seaborn as snsA beautiful report
With a plotly bug
import plotly as pl
print(pl.__version__)5.11.0
# without including this, the plotly charts will not render correctly!
# cf. https://quarto.org/docs/interactive/widgets/jupyter.html
# and https://plotly.com/python/renderers/#setting-the-default-renderer
import plotly.io as pio
pio.renderers.default = "notebook"
# NOTE: as of April 2023, Quarto+Jupyter only works correctly ipynb notebooks created using an environment with plotly<=5.11
# if you use plotly>=5.12 in your venv, Plotly charts will not render in quarto documents,
# producing error "plotly.js not found" and the output may be cropped
# tbd: submit an issue to Quarto Load data:
data = sns.load_dataset('penguins')
data_agg = data.groupby(['species','island','sex'])['body_mass_g'].count().rename('count').reset_index()
data_agg2 = data_agg.groupby('species')['count'].sum().reset_index()Plotly
Let’s make a plotly chart. This will be Figure 1.
datatrace = {
'type': 'bar',
'x': data_agg2['species'],
'y': data_agg2['count']
}
layout = {
'title': {'text': 'Penguin counts', 'x': 0.5},
'margin': {'t': 80, 'b': 20, 'r': 20, 'l': 20},
'height': 250,
'width': 300,
'modebar': { 'orientation': 'h',}
}
figdict = {'data': datatrace,
'layout': layout}
fp = go.Figure(**figdict)fp.show()
Matplotlib
f, ax = plt.subplots(figsize=(4,4))
for sp in data['species'].unique():
tmp_data = data.query('species == @sp')
ax.scatter(x=tmp_data['flipper_length_mm'], y=tmp_data['body_mass_g'], label=sp)
ax.set_title('Penguin measurements');
ax.set_ylabel('Body Mass [g]');
ax.set_xlabel('Flipper Length [mm]');
l = ax.legend(bbox_to_anchor=(0.0, 1.0), loc='upper left');
l.set_frame_on(False)